﻿using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.Mysql;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace MDT.LifeSewagePlatform.Repository
{
    public class HandleBaseRepository<TEntity>:BaseRepository<TEntity> where TEntity: HandleEntity
    {
        public override async ValueTask<EntityEntry<TEntity>> Create(TEntity entity, int CreatorId)
        {
            entity.CreationTime = DateTime.Now;
            entity.CreatorId = CreatorId;
            entity.ConcurrencyStamp = Guid.NewGuid();
            entity.IsDelete = false;
            entity.LastModificationTime = DateTime.Now;
            entity.LastModifierId = CreatorId;
            return await _dbContext.Set<TEntity>().AddAsync(entity);
        }

        public override async Task<List<TEntity>> Create(List<TEntity> entitys, int CreatorId)
        {
            foreach (var entity in entitys)
            {
               
                entity.CreationTime = DateTime.Now;
                entity.CreatorId = CreatorId;
                entity.ConcurrencyStamp = Guid.NewGuid();
                entity.IsDelete = false;
                entity.LastModificationTime = DateTime.Now;
                entity.LastModifierId = CreatorId;
            }
            await _dbContext.Set<TEntity>().AddRangeAsync(entitys.ToArray());
            return entitys;

        }

        public override async Task<EntityEntry<TEntity>> Update(TEntity entity, int updateid)
        {
            //
            return await Task.Run(()=> { 
             entity.ConcurrencyStamp = Guid.NewGuid();
            entity.LastModificationTime = DateTime.Now;
            entity.LastModifierId = updateid;
            return _dbContext.Set<TEntity>().Update(entity);
            });
        }
        public override async Task Update(List<TEntity> entitys, int updateid)
        {
            //
            await Task.Run(() =>
            {
                foreach (var entity in entitys)
                {
                    entity.ConcurrencyStamp = Guid.NewGuid();
                    entity.LastModificationTime = DateTime.Now;
                    entity.LastModifierId = updateid;
                }
                _dbContext.Set<TEntity>().UpdateRange(entitys);
            });
          
        }
       
        public override async Task SoftDelete(Expression<Func<TEntity, bool>> lamada,int deleterId)
        {
            var entities = await _dbContext.Set<TEntity>().Where(lamada).ToListAsync();
            foreach (var item in entities)
            {
                item.ConcurrencyStamp = Guid.NewGuid();
                item.DeletionTime = DateTime.Now;
                item.IsDelete = true;
                item.DeleterId = deleterId;
            }
            _dbContext.Set<TEntity>().UpdateRange(entities);
        }
    }
}
